//
//  CheckLoggedIn.m
//  KotnetAutologin
//
//	check if user is logged in
//
//  Created by Kristof Marien on 6/01/10.
//  Copyright 2010 KotnetAutologin. All rights reserved.
//

#import "CheckLoggedIn.h"


@implementation CheckLoggedIn

@synthesize delegate;
@synthesize loggedInCallback;
@synthesize notloggedInCallback;

/**
 *  starts checking
 */
- (void) checkLoggedIn:(id)loggedInDelegate loggedInSelector:(SEL)loggedInSelector notloggedInSelector:(SEL)notloggedInSelector
{
	self.delegate = loggedInDelegate;
	self.loggedInCallback = loggedInSelector;
	self.notloggedInCallback = notloggedInSelector;
	
	source = [GetHTMLSource alloc];
	source.url = @"http://www.google.be";
	[source getSource:self receivedSelector:@selector(sourceReceived:) errorSelector:@selector(sourceError)];
}
/**
 *  check the source if user is logged in
 */
- (void) sourceReceived:(NSString *)data
{
	[source release];
	NSRange range = [data rangeOfString:@"netlogin.kuleuven.be"];
	if (range.location == NSNotFound) {
		if (self.delegate && self.loggedInCallback) {
			if ([self.delegate respondsToSelector:self.loggedInCallback]) {
				[self.delegate performSelector:self.loggedInCallback];
			}
		}
	} else {
		if (self.delegate && self.notloggedInCallback) {
			if ([self.delegate respondsToSelector:self.notloggedInCallback]) {
				[self.delegate performSelector:self.notloggedInCallback];
			}
		}
	}
}
/**
 *  error while loading source
 */
- (void) sourceError
{
	[source release];
	NSLog(@"Error getting source");
	if (self.delegate && self.notloggedInCallback) {
		if ([self.delegate respondsToSelector:self.notloggedInCallback]) {
			[self.delegate performSelector:self.notloggedInCallback];
		}
	}
}

@end
